declare let window: any;
const win = window.navigator as any;

export default class Browser{
    
    /**
     * 获取浏览器类型
     * @returns 
     */
    public static getBrowserType():string{
        // 获取浏览器 userAgent
        var ua = navigator.userAgent
        // 是否为 Opera
        var isOpera = ua.indexOf('Opera') > -1
        // 返回结果
        if (isOpera) { return 'Opera' }
        // 是否为 IE
        var isIE = (ua.indexOf('compatible') > -1) && (ua.indexOf('MSIE') > -1) && !isOpera
        var isIE11 = (ua.indexOf('Trident') > -1) && (ua.indexOf("rv:11.0") > -1)
        // 返回结果
        if (isIE11) { return 'IE11'
        } else if (isIE) {
        // 检测是否匹配
        var re = new RegExp('MSIE (\\d+\\.\\d+);')
        re.test(ua)
        // 获取版本
        var ver = parseFloat(RegExp["$1"])
        if (ver == 7) { return 'IE7'
            } else if (ver == 8) { return 'IE8'
            } else if (ver == 9) { return 'IE9'
            } else if (ver == 10) { return 'IE10'
            } else { return "IE" }
        }
        var isEdge = ua.indexOf("Edge") > -1
        if (isEdge) { return 'Edge' }
        var isFirefox = ua.indexOf("Firefox") > -1
        if (isFirefox) { return 'Firefox' }
        var isSafari = (ua.indexOf("Safari") > -1) && (ua.indexOf("Chrome") == -1)
        if (isSafari) { return "Safari" }
        var isChrome = (ua.indexOf("Chrome") > -1) && (ua.indexOf("Safari") > -1) && (ua.indexOf("Edge") == -1)
        if (isChrome) { return 'Chrome' }
        var isUC= ua.indexOf("UBrowser") > -1
        if (isUC) { return 'UC' }
        var isQQ= ua.indexOf("QQBrowser") > -1
        if (isQQ) { return 'QQ' }
        return '';
    }

    /**
     * 设置 getUserMedia 版本兼容（ navigator.getUserMedia 或 window.navigator.mediaDevices.getUserMedia）
     */
    public static initUserMedia() {
    if (win.mediaDevices === undefined) {
        win.mediaDevices = {};
    }
    if (win.mediaDevices.getUserMedia === undefined) {
        win.mediaDevices.getUserMedia = function(constraints:any) {
            let getUserMedia = win.getUserMedia || win.webkitGetUserMedia || win.mozGetUserMedia;
            if (!getUserMedia) {
                return Promise.reject(new Error(Browser.getBrowserType()+'浏览器不支持 getUserMedia'));
            }

            return new Promise(function(resolve:any, reject:any) {
                getUserMedia.call(win, constraints, resolve, reject);
            });
        }
    }
    }


    /**
     * 判断是否为小端字节序
     * @returns 
     */
    public static isLittleEdian():boolean{
         // 判断端字节序
         return (function() {
            let buffer = new ArrayBuffer(2);
            new DataView(buffer).setInt16(0, 256, true);
            return new Int16Array(buffer)[0] === 256;
        })();
    }
  
}